package chapter8.section6;

import java.util.*;

/**
 * 程序 8-9b 利用排序集合保存实现了排序接口的 MyAccount2 对象
 */

class MyAccount2 {
    private final String id; // 用户唯一的ID
    private final String name;// 用户名称
    private final int balance;// 当前余额

    public MyAccount2(String id, String name, int balance) {
        super();
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    public static Comparator<MyAccount2> SortByIdDesc = new Comparator<MyAccount2>() {
        @Override
        public int compare(MyAccount2 a, MyAccount2 b) {
            return b.id.compareTo(a.getId());
        }
    };

    public String getId() {
        return id;
    }

    // 打印
    @Override
    public String toString() {
        return "MyAccount2[id:" + id + ", name:" + name + ", balance:" + balance + "]";
    }
}

public class MySortedAccountRoster2 {
    public static void main(String[] args) {

        List<MyAccount2> list = new ArrayList<>();
        list.add(new MyAccount2("001", "张三", 3000));
        list.add(new MyAccount2("003", "李四", 1000));
        list.add(new MyAccount2("002", "王五", 2000));
        System.out.println("--列出当前所有的账户，注意输出顺序和添加时的顺序--");
        
        // 1. 指定排序方法
        Collections.sort(list, MyAccount2.SortByIdDesc);
        
        Iterator<MyAccount2> it = list.iterator();
        while (it.hasNext()) {
            MyAccount2 account = it.next();
            System.out.println(account);
        }
    }
}
